<template>
  <div class="login-container">
    <!-- 顶部 -->
    <!-- <div class="absolute top-0 flex items-center justify-end px-5 h-20 w-full">
      <el-switch
        v-model="isDark"
        inline-prompt
        :active-icon="IconEpMoon"
        :inactive-icon="IconEpSunny"
        active-color="var(--el-fill-color-dark)"
        inactive-color="var(--el-color-primary)"
        @change="handleThemeChange"
      />
      <lang-select class="ml-2 cursor-pointer" />
    </div> -->
    <!-- 登录表单 -->
    <div class="left">
      <div class="title">
        <img src="../../assets/logotop.png" alt="">
      </div>
    </div>

    <div class="right">
      <div class="loginform">
        <p style="font-size: 40px;font-weight: 400;">欢迎登录</p>
        <el-form
        ref="loginFormRef"
        :model="loginData"
        :rules="loginRules"
         class="login-form"
          autocomplete="on"
          label-position="left"
        >

          <el-form-item prop="username" style="margin-bottom: 20px;">
            <el-input
              ref="username"
              v-model="loginData.username"
              placeholder="用户名"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
              style="padding-left: 10px;"
            >
            <template #prefix>
    	         <i-ep-user  style="margin-right: 30px;margin-left: 10px;"/>
             </template>
            </el-input>
          </el-form-item>

          <el-tooltip  :disabled="isCapslock === false" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <el-input                
                ref="password"
                v-model="loginData.password"
                :type="passwordVisible === false ? 'password' : 'input'"
                placeholder="密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                prefix-icon="el-icon-lock"
                @keyup.native="checkCapslock"
                @keyup.enter.native="handleLogin"
              >
              <template #prefix>
    	         <i-ep-lock />
             </template>
              </el-input>
            </el-form-item>
          </el-tooltip>

          <div class="bigbox" style="display: flex;align-items: center;justify-content: space-between;">
            <el-form-item prop="yzcode" style="width: 250px;">
              <el-input
             
                ref="yzcode"
                v-model="loginData.yzcode"
                placeholder="图形验证码"
                name="yzcode"
                tabindex="2"
                autocomplete="on"
                prefix-icon="el-icon-edit"
               
                @keyup.enter.native="handleLogin"
              >
              <template #prefix>
    	         <i-ep-edit />
             </template>
              </el-input>
            </el-form-item>
            <div
              class="yzcode"
              style="width: 100px;height: 50px;border-radius: 5px;margin-top: -20px; border: 1px solid lightgray; float: right;"
            />
          </div>

          <el-form-item prop="goole">
            <el-input
             
              ref="goole"
              v-model="loginData.password"
            
              placeholder="谷歌验证码(没有可以不填)"
              name="goole"
              tabindex="2"
              autocomplete="on"
              prefix-icon="el-icon-message"
              @keyup.native="checkCapslock"
            
              @keyup.enter.native="handleLogin"
            >
            <template #prefix>
    	         <i-ep-message />
             </template>
            </el-input>
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;height: 50px; margin-bottom:20px;background-color: rgba(0, 153, 136, 1);border-radius: 20px;font-size: 18px;margin-top: 20px;"
            @click.native.prevent="handleLogin"
          >登录</el-button>
        </el-form>
      </div>
    </div>


       <!--  <el-form
        ref="loginFormRef"
        :model="loginData"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <div class="p-2">
            <svg-icon icon-class="user" />
          </div>
          <el-input
            ref="username"
            v-model="loginData.username"
            class="flex-1"
            size="large"
            :placeholder="$t('login.username')"
            name="username"
          />
        </el-form-item>

        <el-tooltip
          :disabled="isCapslock === false"
          content="Caps lock is On"
          placement="right"
        >
          <el-form-item prop="password">
            <span class="p-2">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              v-model="loginData.password"
              class="flex-1"
              :placeholder="$t('login.password')"
              :type="passwordVisible === false ? 'password' : 'input'"
              size="large"
              name="password"
              @keyup="checkCapslock"
              @keyup.enter="handleLogin"
            />
            <span class="mr-2" @click="passwordVisible = !passwordVisible">
              <svg-icon
                :icon-class="passwordVisible === false ? 'eye' : 'eye-open'"
                class="cursor-pointer"
              />
            </span>
          </el-form-item>
        </el-tooltip> -->

        <!-- 验证码 -->
       <!--  <el-form-item prop="captchaCode">
          <span class="p-2">
            <svg-icon icon-class="captcha" />
          </span>

          <el-input
            v-model="loginData.captchaCode"
            auto-complete="off"
            :placeholder="$t('login.captchaCode')"
            class="w-[60%]"
            @keyup.enter="handleLogin"
          />

          <div class="captcha">
            <el-image
              :src="captchaBase64"
              @click="getCaptcha"
              class="w-[120px] h-[48px] cursor-pointer"
            >
              <template #error>
                <div class="image-slot">
                  <i-ep-picture />
                </div>
              </template>
            </el-image>
          </div>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          class="w-full"
          @click.prevent="handleLogin"
          >登录
        </el-button>

      </el-form> -->
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import router from "@/router";
import LangSelect from "@/components/LangSelect/index.vue";
import SvgIcon from "@/components/SvgIcon/index.vue";
import IconEpSunny from "~icons/ep/sunny";
import IconEpMoon from "~icons/ep/moon";
import { useSettingsStore } from "@/store/modules/settings";
import {User} from '@element-plus/icons-vue'

// 状态管理依赖
import { useUserStore } from "@/store/modules/user";
import { useAppStore } from "@/store/modules/app";

// API依赖
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router";
import { getCaptchaApi } from "@/api/auth";
import { LoginData } from "@/api/auth/types";

const settingsStore = useSettingsStore();

const { title, version } = settingsStore;
/**
 * 明亮/暗黑主题切换
 */
const isDark = ref<boolean>(settingsStore.theme === "dark");
const handleThemeChange = (isDark: any) => {
  useToggle(isDark);
  settingsStore.changeSetting({
    key: "theme",
    value: isDark ? "dark" : "light",
  });
};

/**
 * 根据屏幕宽度切换设备模式
 */
const appStore = useAppStore();
const WIDTH = 992; // 响应式布局容器固定宽度  大屏（>=1200px） 中屏（>=992px） 小屏（>=768px）
const { width } = useWindowSize();
watchEffect(() => {
  if (width.value < WIDTH) {
    appStore.toggleDevice("mobile");
  } else {
    appStore.toggleDevice("desktop");
  }
});

const loading = ref(false); // 按钮loading
const isCapslock = ref(false); // 是否大写锁定
const passwordVisible = ref(false); // 密码是否可见
const captchaBase64 = ref(); // 验证码图片Base64字符串
const loginFormRef = ref(ElForm); // 登录表单ref

const loginData = ref<LoginData>({
  username: "admin",
  password: "123456",
  yzcode:'11'
});

const { t } = useI18n();
const loginRules = computed(() => {
  const prefix = appStore.language === "en" ? "Please enter " : "请输入";
  return {
    username: [
      {
        required: true,
        trigger: "blur",
        message: `${prefix}${t("login.username")}`,
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        validator: (rule: any, value: any, callback: any) => {
          if (value.length < 6) {
            callback(new Error("The password can not be less than 6 digits"));
          } else {
            callback();
          }
        },
        message: `${prefix}${t("login.password")}`,
      },
    ],
    captchaCode: [
      {
        required: true,
        trigger: "blur",
        message: `${prefix}${t("login.captchaCode")}`,
      },
    ],
  };
});

/**
 * 检查输入大小写状态
 */
function checkCapslock(e: any) {
  const { key } = e;
  isCapslock.value = key && key.length === 1 && key >= "A" && key <= "Z";
}

/**
 * 获取验证码
 */
function getCaptcha() {
  getCaptchaApi().then(({ data }) => {
    loginData.value.captchaKey = data.captchaKey;
    captchaBase64.value = data.captchaBase64;
  });
}

/**
 * 登录
 */
const route = useRoute();
const userStore = useUserStore();
function handleLogin() {
  loginFormRef.value.validate((valid: boolean) => {
        if (valid) {
          loading.value = true;

          userStore.login(loginData.value)
            .then(() => {
              const query = route.query;
              const redirect = (query.redirect as string) || '/';

              const otherQueryParams = Object.keys(query).reduce((acc: any, cur: string) => {
                if (cur !== 'redirect') {
                  acc[cur] = query[cur];
                }
                return acc;
              }, {});

              router.push({ path: redirect, query: otherQueryParams });
            })
            .catch(() => {
              // Handle login failure, e.g., refresh captcha
              getCaptcha();
            })
            .finally(() => {
              loading.value = false;
            });
        
     
          }});
    
  };


onMounted(() => {
  getCaptcha();

  // 主题初始化
  const theme = useSettingsStore().theme;
  useSettingsStore().changeSetting({ key: "theme", value: theme });
  if (theme == "dark") {
    document.documentElement.classList.add("dark");
  } else {
    document.documentElement.classList.remove("dark");
  }
});
</script>

<style lang="scss" scoped>
.dark .login-container {
  background: url("@/assets/images/login-bg-dark.jpg") no-repeat center right;
}

.login-container {
 // @apply w-full h-full flex-center;

  overflow-y: auto;
  //background: url("@/assets/images/login-bg.jpg") no-repeat center right;

  .login-form {
    padding: 30px 10px;

    .captcha {
      position: absolute;
      top: 0;
      right: 0;

      .image-slot {
        display: flex;
        align-items: center;
        justify-content: right;
        width: 100%;
        height: 100%;
        font-size: 18px;
        color: var(--el-text-color-secondary);
        background: var(--el-fill-color-light);

        svg {
          margin-right: 10px;
        }
      }
    }
  }
}

.el-form-item {
  background: var(--el-input-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 5px;
}

:deep(.el-input) {
  .el-input__wrapper {
    padding: 0;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      transition: background-color 1000s ease-in-out 0s; /* 通过延时渲染背景色变相去除背景颜色 */
    }
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 50px;
    width: 85%;
    display: flex;
    align-items: center;
    padding-left: 20px;
    box-sizing: border-box;

     input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 60px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(245, 246, 248, 1);
    border-radius: 5px;
    color: white;
   // margin-top: 20px;
    border: 1px solid lightgray;
  }
}
.login-container {
  width: 100%;
  height: 100%;
  display: flex;

  .left {
    width: 60%;
    height: 100%;
    //background-color: pink;
    background-image: url('../../assets/封面.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    padding-top: 84px;
    box-sizing: border-box;

    .title {
      width: 180px;
      height: 60px;
      margin-left: 107px;
    }
  }

  .right {
    display: flex;
    align-items: center;
    margin: 0 auto;

    .loginform {
      width: 400px;
      height: 500px;
      margin-top: -100px;
      //background-color: skyblue;
    }

  }

}


.el-input__prepend .el-icon {  
  border: none !important;  
}
</style>
